package com.ruoyi.change.pay.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.basic.service.IBaseService;
import com.param.concat.IUpdatePaymentParam;
import com.ruoyi.center.domain.NoticeCenter;
import com.ruoyi.center.service.INoticeCenterService;
import com.ruoyi.change.pay.IPayStatus;
import com.ruoyi.configuration.domain.MgTeamSigningRewards;
import com.ruoyi.configuration.service.IMgTeamSigningRewardsService;
import com.ruoyi.order.domain.MgOrder;
import com.ruoyi.order.domain.OrderLog;
import com.ruoyi.order.service.IMgOrderService;
import com.ruoyi.order.service.IOrderLogService;
import com.ruoyi.pay.manager.domain.PayManager;
import com.ruoyi.pay.manager.service.IPayManagerService;
import com.ruoyi.team.domain.vo.MgTeam;
import com.ruoyi.team.service.ITeamInformationService;
import com.ruoyi.user.domain.MgUser;
import com.ruoyi.user.service.IMgUserService;

/**
 * 已支付
 * 
 * @param <T>
 */
public class AlreadyPay<T> extends AbstractPayStatus<T> implements IPayStatus {

	/**
	 * 0 --> 主表
	 * 
	 * 1 --> 从
	 * 
	 * 2 ---> 其他主表 （例如 订单 、用户）
	 * 
	 * @param baseService
	 */
	public AlreadyPay(IBaseService<T>[] baseService) {
		super(baseService);
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int update(IUpdatePaymentParam managerParam) {

		IBaseService<T>[] baseService = this.getBaseService();

		IPayManagerService payManagerService = (IPayManagerService) baseService[0];

		int id = managerParam.getId();

		PayManager manager = (PayManager) payManagerService.selectById(id);

		if (manager == null) {
			return -1;
		}

		payManagerService.updatePayStatus(managerParam);

		// 查询子订单
//      PayManager manager = payManagerService.selectById(id);

//		IBaseService<T> managerService = baseService[1];
//		
//		managerService.updatePayStatus(managerParam);
//		
//		
//		List<T> selectListById = managerService.selectListById(managerParam.getId());
//      
		// 添加到订单商品表

		IMgOrderService orderService = (IMgOrderService) baseService[1];

		List<MgOrder> selectOrderByPayManagerIdForUpdate = orderService
				.selectOrderByPayManagerIdForUpdate(new int[] { id });

		int batchInsert = orderService.updateAlreadyPay(managerParam);

		// 添加日志

		IOrderLogService orderLogService = (IOrderLogService) baseService[2];

		List<OrderLog> orderLogs = new ArrayList<OrderLog>();

		OrderLog item;
		
		NoticeCenter noticeCenter;
		
		List<NoticeCenter> noticeCenters = new ArrayList<NoticeCenter>();

		long timeMillis = System.currentTimeMillis() / 1000L;
		Integer orderId;
		for (MgOrder mgOrder : selectOrderByPayManagerIdForUpdate) {

			item = new OrderLog();

			orderId = mgOrder.getId();
			
			item.setOrderId(orderId);

			item.setPayManagerId(mgOrder.getPayManagerId());

			item.setOrderStatus(15);

			item.setUserId(mgOrder.getUserId().intValue());

			item.setRequirement("");

			item.setType(2);

			item.setCreateTime(timeMillis);

			orderLogs.add(item);
			
			noticeCenter = new NoticeCenter();
			
			noticeCenter.setUserId(manager.getUserId());
			
			noticeCenter.setTitle("派稿通知  ");
			
			noticeCenter.setNoticeContent("【"+ mgOrder.getServiceTypeName() +"】订单"+ mgOrder.getOrderSnId() +"进入待派稿，请及时查看处理!");
			
			noticeCenter.setJump((byte) 1);
			
			noticeCenter.setNoticeType(0);
			
			noticeCenter.setPlatform(1);
			
			noticeCenter.setReadStatus(0);
			
			noticeCenter.setTranslateType((byte) 0);
			
			noticeCenter.setTypeId(orderId);
			
			noticeCenters.add(noticeCenter);
		}

		orderLogService.batchInsert(orderLogs);

		// 添加 管理员通知
		
		INoticeCenterService noticeCenterService =  (INoticeCenterService) baseService[6];
		
		noticeCenterService.batchInsert(noticeCenters);
		
		
		return batchInsert;
	}

	int updateTeamDiscount(IBaseService<T>[] baseService, PayManager manager) {
		// 更新团队折扣

		IMgUserService userService = (IMgUserService) baseService[3];

		MgUser selectById = userService.selectById(manager.getUserId());

		if (0 == selectById.getTeamId()) { // 您不是团员
			return 1000000000;
		}

		IMgTeamSigningRewardsService mgTeamSigningRewardsService = (IMgTeamSigningRewardsService) baseService[4];

		List<MgTeamSigningRewards> selectList = mgTeamSigningRewardsService
				.selectMgTeamSigningRewardsList(new MgTeamSigningRewards());
//				
		selectList.sort((f, s) -> s.getPrice().compareTo(f.getPrice()));

		Long teamId = selectById.getTeamId();
		IPayManagerService payManagerService = (IPayManagerService) baseService[0];
		BigDecimal sumPriceByTeam = payManagerService .sumPriceByTeam(teamId), price = null;

		MgTeamSigningRewards signingRewards = null;

		for (MgTeamSigningRewards rewards : selectList) {

//					Byte type = rewards.getType();

			price = rewards.getPrice();

//					if (1 == type) {
//						// 常规
//						if(sumPriceByTeam.equals(price)) {
//							
//							signingRewards = rewards;
//							
//							break;
//						}
//						
//					} else if (0 == type) {
			// 以上
			if (0 < sumPriceByTeam.compareTo(price)) {
				signingRewards = rewards;
				break;
			}
//					}
		}

		if (null == signingRewards) {
			return 1000000000;
		}

		ITeamInformationService teamService = (ITeamInformationService) baseService[5];

		MgTeam update = new MgTeam();

		update.setId(teamId.intValue());

		update.setDiscount(signingRewards.getDiscount());

		teamService.updateMgTeam(update);
		
		return 1;
	}

}
